package com.wbcs.system.component.application.report.configbean.crosslist;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.wbcs.config.component.application.report.AbsConfigBean;
import com.wbcs.config.component.application.report.ReportDataSetBean;
import com.wbcs.config.component.application.report.extendconfig.AbsExtendConfigBean;
import com.wbcs.exception.WbcsConfigLoadingException;

public class CrossListReportBean extends AbsExtendConfigBean
{
    public final static String EMPTY_DATASET_ID="WX_EMPTY_DATASET_ID";

    private boolean hasDynamicColGroupBean;

    private boolean shouldCreateRowSelectCol;//当前交叉报表是否需要在运行时新增行选中列（因为这种报表是动态查询要显示的列的，因此如果是checkbox/radiobox行选中类型，且没有配置行选中列，则不能在加载时生成，而是在运行时生成）

    private Map<String,CrossListReportDynDatasetBean> mCrossDatasetBeans;

    public CrossListReportBean(AbsConfigBean owner)
    {
        super(owner);
    }

    public boolean isHasDynamicColGroupBean()
    {
        return hasDynamicColGroupBean;
    }

    public void setHasDynamicColGroupBean(boolean hasDynamicColGroupBean)
    {
        this.hasDynamicColGroupBean=hasDynamicColGroupBean;
    }

    public boolean isShouldCreateRowSelectCol()
    {
        return shouldCreateRowSelectCol;
    }

    public void setShouldCreateRowSelectCol(boolean shouldCreateRowSelectCol)
    {
        this.shouldCreateRowSelectCol=shouldCreateRowSelectCol;
    }

    public CrossListReportDynDatasetBean getCrossDatasetBean(AbsCrossListReportColAndGroupBean crossColGroupBean,boolean isCreateNewWhileNull)
    {
        String datasetid=crossColGroupBean.getDatasetid();
        datasetid=datasetid==null||datasetid.trim().equals("")?EMPTY_DATASET_ID:datasetid.trim();
        if(this.mCrossDatasetBeans==null) this.mCrossDatasetBeans=new HashMap<String,CrossListReportDynDatasetBean>();
        CrossListReportDynDatasetBean crossDatasetbean=this.mCrossDatasetBeans.get(datasetid);
        if(crossDatasetbean==null&&isCreateNewWhileNull)
        {
            crossDatasetbean=new CrossListReportDynDatasetBean();
            ReportDataSetBean datasetbean=this.getOwner().getReportBean().getSbean().getDatasetBeanById(crossColGroupBean.getDatasetid());
            if(datasetbean==null)
            {
                throw new WbcsConfigLoadingException("加载报表"+this.getOwner().getReportBean().getPath()+"失败，根据它的datasetid："
                        +crossColGroupBean.getDatasetid()+"在<sql/>中没有取到对应的数据集");
            }
            crossDatasetbean.setDatasetbean(datasetbean);
            this.mCrossDatasetBeans.put(datasetid,crossDatasetbean);
        }
        return crossDatasetbean;
    }

    public void doPostLoad()
    {
        if(mCrossDatasetBeans==null) return;
        for(Entry<String,CrossListReportDynDatasetBean> entryTmp:mCrossDatasetBeans.entrySet())
        {
            entryTmp.getValue().doPostLoad();
        }
    }
}
